package com.dkd.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.core.page.TableDataInfo;
import com.dkd.common.enums.BusinessType;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.manage.domain.Dto.ChannelConfigDto;
import com.dkd.manage.domain.Vo.ChannelVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Vo.ChannelVo;
import com.dkd.manage.service.IChannelService;

/**
 * 售货机货道Controller
 *
 * @author dcg
 * @date 2025-07-15
 */
@RestController
@RequestMapping("/manage/channel")
public class ChannelController extends BaseController {
    @Autowired
    private IChannelService channelService;

    /**
     * 查询售货机货道列表
     */
    @PreAuthorize("@ss.hasPermi('manage:channel:list')")
    @GetMapping("/list")
    public TableDataInfo list(Channel channel) {
        startPage();
        List<Channel> list = channelService.selectChannelList(channel);
        return getDataTable(list);
    }

    /**
     * 导出售货机货道列表
     */
    @PreAuthorize("@ss.hasPermi('manage:channel:export')")
    @Log(title = "售货机货道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Channel channel) {
        List<Channel> list = channelService.selectChannelList(channel);
        ExcelUtil<Channel> util = new ExcelUtil<Channel>(Channel.class);
        util.exportExcel(response, list, "售货机货道数据");
    }

    /**
     * 获取售货机货道详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:channel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(channelService.selectChannelById(id));
    }

    /**
     * 新增售货机货道
     */
    @PreAuthorize("@ss.hasPermi('manage:channel:add')")
    @Log(title = "售货机货道", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Channel channel) {
        return toAjax(channelService.insertChannel(channel));
    }

    /**
     * 修改售货机货道
     */
    @PreAuthorize("@ss.hasPermi('manage:channel:edit')")
    @Log(title = "售货机货道", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Channel channel) {
        return toAjax(channelService.updateChannel(channel));
    }

    /**
     * 删除售货机货道
     */
    @PreAuthorize("@ss.hasPermi('manage:channel:remove')")
    @Log(title = "售货机货道", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(channelService.deleteChannelByIds(ids));
    }


    /**
     * 根据售货机编号查询货道列表
     *
     * @param innerCode 货道编码
     * @return AjaxResult 返回的是数据
     */
    @GetMapping("/list/{innerCode}")
    @PreAuthorize("@ss.hasPermi('manage:channel:list')")
    public AjaxResult listByInnerCode(@PathVariable String innerCode) {
        return success(channelService.selectChannelVoByInnerCode(innerCode));
    }


    /**
     * 货道商品的批量插入
     * @param channelConfigDto
     * @return AjaxResult 返回影响的行数来判断更新的成功/失败
     * 思路：
     *      1. 前端发来货道以及与货道关联的数据，所以需要一个Dto接收，而我们的ChannelConfigDto就是干这个的事情
     *      2. 我们要把dto的数据抽出对应的一个货道信息（ChannelSkuDto），那么我们知道有多个货道，所以需要一个list来保存
     *      3. 抽出来的每一个对象就是我们的po对象，即每一个channel货道对象就是我们实例化的对象
     *      4. 而这些对象我们可以通过设备编码+货道编码确定唯一的对象，即我们的selectChannelVoByInnerCodeAndChannelCode方法
     *      5. 所以，我们只需要将这些对象通过集合存起来，进行批量更新即可，实现这个功能的是batchUpdateChannel
     */
    @PutMapping("/config")
    @PreAuthorize("@ss.hasPermi('manage:channel:config')")
    public AjaxResult setChannel(@RequestBody ChannelConfigDto channelConfigDto) {
        // 因为返回的是影响行数，而success是数据
        return toAjax(channelService.setChannel(channelConfigDto));
    }
}
